package com.itheima.demo01;

import java.lang.reflect.Field;

public class Test {
    public static void main(String[] args) throws IllegalAccessException {
        System.out.println("这是java类反射");
        Person person = new Person();
        person.setAge(100);
        person.setName("黎倡华");
        person.setHeight(185);
        person.setSex("y");
        TestFields(person);
    }

    /**
     * author:xdx
     * email:1125982465@qq.com
     * phone:13274569895
     */

    public static void TestFields(Person person) throws IllegalAccessException {
        Class<?> clazz = person.getClass();
        Field[] fields = clazz.getDeclaredFields(); // 注意这里使用了getDeclaredFields()
        for (Field field : fields) {
            field.setAccessible(true); // 必须设置为true才能访问private字段
            System.out.println(field.getName() + ":" + field.get(person) + "\t");
        }
    }
}